﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atom.cache;
using Atom.dao.Model;
using Atom.NetFrame;

namespace Atom.biz.impl
{
    public class UserBiz : IUserBiz
    {
        private IAccountBiz accountBiz = BizFactory.AccountBiz;
        private IUserCache userCache = CacheFactory.UserCache;
        public bool Create(UserToken ut, string name)
        {
            //验证账号是否存在，获取账号ID
            int accountID = accountBiz.GetID(ut);
            if (accountID == -1) return false;
            //判断当前账号是否拥有角色
            if (userCache.HasByAccountID(accountID)) return false;
            userCache.Create(ut, name, accountID);
            return true;
        }


        public UserMode GetUser(int id)
        {

            return userCache.GetUser(id);
        }

        public UserMode GetUser(UserToken ut)
        {
            return userCache.GetUser(ut);
        }


        public void OffLine(UserToken ut)
        {
            userCache.OffLine(ut);
        }

        public UserMode OnLine(UserToken ut)
        {
            //验证账号是否存在，获取账号ID
            int accountID = accountBiz.GetID(ut);
            if (accountID == -1) return null;
            UserMode user = userCache.GetUserByAccountID(accountID);
            if (user == null) return null;
            //判断当前账号是否拥有角色
            if (userCache.isOnLine(user.ID)) return null;
            //在线
            userCache.OnLine(ut, user.ID);
            return user;
        }

        public UserToken GetToken(int id)
        {
            return userCache.GetToken(id);
        }

        public UserMode GetUserOnGetInfo(UserToken ut)
        {
            //验证账号是否存在，获取账号ID
            int accountID = accountBiz.GetID(ut);
            if (accountID == -1) return null;
            return userCache.GetUserByAccountID(accountID);
        }
    }
}
